#include "stack.h"
#include"stdlib.h"
#include<assert.h>



void StackInit(Stack* ps)
{
    assert(ps);
    ps->_a = nullptr;
    ps->_top = ps->_capacity = 0;
}

void StackPush(Stack* ps, STDataType data)
{
    assert(ps);
    if (ps->_capacity == ps->_top)
    {
        int newcapacity = ps->_capacity == 0 ? 4 : 2 * ps->_capacity;
        STDataType* tmp = new STDataType[newcapacity];
        ps->_a = tmp;
        ps->_capacity = newcapacity;
        if (ps->_a)
        {
            for (int i = 0; i < ps->_top; i++)
            {
                tmp[i] = ps->_a[i];
            }
            delete[]ps->_a;
        }
    }
    ps->_a[ps->_top] = data;
    ps->_top++;
}

void StackPop(Stack* ps)
{
    ps->_top--;
}

STDataType StackTop(Stack* ps)
{
    assert(ps && ps->_top > 0);
    return ps->_a[ps->_top - 1];
}

int StackSize(Stack* ps)
{
    assert(ps);
    return ps->_top;
}

int StackEmpty(Stack* ps)
{
    assert(ps);
    return ps->_top==0;
}

void StackDestroy(Stack* ps)
{
    delete[]ps->_a;
    ps->_a = nullptr;
    ps->_capacity = ps->_top = 0;
}
